package com.ican.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ican.common.PropertyVo;

public class NcSysConfigUtil {
    private final static Logger logger = LoggerFactory.getLogger(NcSysConfigUtil.class);
    
    private static Properties props;
    
    private static Properties getProperties(){
        if(props == null){
            FileInputStream in = null;
            try {
                String filePath = NcSysConfigUtil.class.getResource("/ncsysconfig.properties").getFile();
                //加载配置
                in = new FileInputStream(filePath);
                props = new Properties();
                props.load(in);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }finally {
                if (null != in){
                    try {
                        in.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
        return props;
    }

    @SuppressWarnings("rawtypes")
    public static List<PropertyVo> getAllProperties() {
        List<PropertyVo> result = new ArrayList<PropertyVo>();
        String key = null;
        PropertyVo pv = null;

        Properties temp = getProperties();
        Enumeration en = temp.propertyNames();
        while (en.hasMoreElements()) {
            key = en.nextElement().toString();
            pv = new PropertyVo(key, temp.getProperty(key));
            result.add(pv);  
        }  
        return result;
    }
    
    public static String getValue(String key) {
		return getProperties().getProperty(key);
	}
}